package com.hdoit.ares.web.cust.controllers;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.cust.CustomerColumn;
import com.hdoit.ares.selfcommon.enums.cust.CustomerLevel;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.JsonActionResult;



/**
 * @author      chenghuan
 * @email       
 * @createtime  2017年10月27日 14:58:02
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/cust/customerColumn/")
public class CustomerColumnController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/cust/customerColumn/";
    }
    private ICustomerService customerService;
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String updateViewName="update";
    private final String pushViewName="push";
   
    
    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model){
        return view(indexViewName);
    }

    @ResponseBody
    @RequestMapping(value = updateViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/json;charset=UTF-8")
    public String update(@RequestBody List<CustomerColumn> customerColumns){
    	for(CustomerColumn customerColumn:customerColumns){
    		customerColumn.setUpdateRegionDate(DateUtils.getCurrent());
    		if(CustomerLevel.maker.equals(customerColumn.getLevel())){
    			customerColumn.setJoinMakerDate(DateUtils.getCurrent());
    		}else if(CustomerLevel.highMaker.equals(customerColumn.getLevel())){
    			customerColumn.setJoinHighMakerDate(DateUtils.getCurrent());
    		}
    	}
    	
    	if(customerService.updateCustomerColumn(customerColumns)){
    		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
    	}
        return  new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @ResponseBody
    @RequestMapping(value = pushViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/json;charset=UTF-8")
    public String push(){
    	//读取指定目录下的xls文件
    	List<CustomerColumn> customerColumns = outputFile("C:/Users/Administrator/Desktop/加入名单.xls");
        return  JsonUtils.writeValueAsString(customerColumns);
    }
    
    
    public List<CustomerColumn> outputFile(String src) {
    	List<CustomerColumn> customerColumns = new ArrayList<CustomerColumn>();
		try {
			File file = new File(src); // 创建文件对象
			Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象（WorkBook）
			Sheet sheet = wb.getSheet(0); // 从工作区中取得页（Sheet）

			Map<String, String> map = new HashMap<String, String>();

			for (int j = 0; j < sheet.getColumns(); j++) {
				Cell cell = sheet.getCell(j, 0);
				map.put(String.valueOf(j), cell.getContents());
			}

			

			for (int i = 1; i < sheet.getRows(); i++) { // 循环打印Excel表中的内容
				CustomerColumn cus = new CustomerColumn();
				for (int j = 0; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(j, i);

					Field field;

					field = cus.getClass().getDeclaredField(map.get(String.valueOf(j)));

					field.setAccessible(true);

					field.set(cus, cell.getContents());

				}
				if (StringUtils.isEmpty(cus.getLevel())) {
					cus.setLevel(CustomerLevel.maker);
				}
				customerColumns.add(cus);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return customerColumns;

	  }
}